package com.zzdz.jdbc.common.exception.enums;

/**
 * <p>
 * ExceptionCodeEnum<br>
 * 异常枚举
 * </p>
 *
 * @author XinLau
 * @version 1.0
 * @since 2019年10月16日 14:30
 */
public enum ExceptionCodeEnum implements ExceptionCode {

    /**
     * 通用未知错误异常
     */
    UNSPECIFIED("500", "服务器内部错误"),
    NO_SERVICE("404", "网络异常, 服务器熔断"),
    REQUEST_PARAM_EXCEPTION("400", "入参异常,请检查入参后再次调用"),
    PROPERTIES_LOAD_EXCEPTION("400001", "Properties - 读取异常"),
    /**
     * JDBC连接工厂异常
     */
    JDBC_FUNCTION_EXCEPTION("400101","JDBC - 运行失败"),
    CONNECTION_OPEN_EXCEPTION("400102","Connection - 开启失败"),
    CONNECTION_CLOSE_EXCEPTION("400103","Connection - 关闭失败"),
    RESULTSET_CLOSE_EXCEPTION("400104","ResultSet - 关闭失败"),
    STATEMENT_CLOSE_EXCEPTION("400105","Statement - 关闭失败"),
    CONNECTION_COMMIT_EXCEPTION("400106","Connection - 提交异常"),
    CONNECTION_ROLLBACK_EXCEPTION("400106","Connection - 回滚异常"),
    /**
     * SQL异常 structuredQueryLanguage
     */
    ANALYSIS_ORIGINAL_SQL_EXCEPTION("400201","StructuredQueryLanguage - Oracle原生SQL解析失败 "),
    ;

    /**
     * 异常代码
     */
    private final String exceptionCode;

    /**
     * 异常描述
     */
    private final String exceptionDescription;

    /**
     * 构造函数
     * @param exceptionCode - 异常代码
     * @param exceptionDescription - 异常描述
     */
    ExceptionCodeEnum(final String exceptionCode, final String exceptionDescription) {
        this.exceptionCode = exceptionCode;
        this.exceptionDescription = exceptionDescription;
    }

    /**
     * 获取异常代码
     * @return String
     */
    @Override
    public String getExceptionCode() {
        return exceptionCode;
    }

    /**
     * 获取异常描述
     * @return String
     */
    @Override
    public String getExceptionDescription() {
        return exceptionDescription;
    }

    /**
     * 根据异常代码查询异常枚举
     * @param exceptionCode 异常代码
     * @return ExceptionCodeEnum - 枚举。
     */
    public static ExceptionCodeEnum getExceptionByExceptionCode(String exceptionCode) {
        for (ExceptionCodeEnum value : ExceptionCodeEnum.values()) {
            if (exceptionCode == null ? value.getExceptionCode() == null : exceptionCode.equals(value.getExceptionCode())) {
                return value;
            }
        }
        return UNSPECIFIED;
    }

    /**
     * 根据异常代码判断是否存在异常代码
     * @param exceptionCode 异常代码
     * @return Boolean
     */
    public static Boolean contains(String exceptionCode){
        for (ExceptionCodeEnum value : ExceptionCodeEnum.values()) {
            if (exceptionCode == null ? value.getExceptionCode() == null : exceptionCode.equals(value.getExceptionCode())) {
                return true;
            }
        }
        return  false;
    }

}
